package com.mrli.cat.weather.activity;

import android.accounts.Account;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;

import com.mrli.cat.weather.database.WeatherContract;
import com.mrli.cat.weather.service.LocationService;
import com.mrli.cat.weather.service.WeatherUpdateService;

/**
 * Created by MrLi on 2015/7/5.
 */
public class NetWorkBroadcast extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activiteNetWork = connectivityManager.getActiveNetworkInfo();
        boolean isCoonected = activiteNetWork != null && activiteNetWork.isConnected();
        if (isCoonected) {
            //请求更新数据
            Account account;
            account = ForecastFragment.CreateSyncAccount(context);
            Bundle settingsBundle = new Bundle();
            settingsBundle.putBoolean(
                    ContentResolver.SYNC_EXTRAS_MANUAL, true);
            settingsBundle.putBoolean(
                    ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
            ContentResolver.requestSync(account, WeatherContract.AUTHORITY, settingsBundle);
            //更新桌面控件
            Intent widget = new Intent(context, WeatherUpdateService.class);
            context.startService(widget);
        }
    }
}
